package com.hockeo.server.data.entity;

import com.hockeo.client.model.JSData;
import com.hockeo.server.data.DAO;
import com.hockeo.server.servlet.convert.GwtEnabled;
import com.hockeo.shared.domain.persistent.Manager;
import com.hockeo.tinygds.TKey;
import com.hockeo.tinygds.annotation.TEntity;
import com.hockeo.tinygds.annotation.TId;
import com.hockeo.tinygds.annotation.TName;
import com.hockeo.tinygds.annotation.TUnindexed;

/**
 * Represents a club that is engaged in the game.
 *
 * @version $Id$
 * @author jjanke
 */
@TEntity
public class Club implements GwtEnabled
{
  @TId
  private String            d_strId;

  @TName( "name" )
  private String            d_strName;

  @TName( "country" )
  private String            d_strCountry;

  @TName( "longitude" )
  @TUnindexed
  private double            d_dLongitude;

  @TName( "latitude" )
  @TUnindexed
  private double            d_dLatitude;

  @TName( "association" )
  private TKey<Association> d_association;

  @TName( "current_manager" )
  private TKey<Manager>     d_currentManager;

  @TName( "current_league" )
  private TKey<Contest>     d_currentLeague;

  /** Argument-less constructor for TinyGDS. */
  private Club()
  {}

  @Override
  public void fromGwt( JSData strSrc )
  {
    d_strId = strSrc.getId();
    d_strName = strSrc.getString( "name" );
    d_strCountry = strSrc.getString( "country" );
    d_dLongitude = strSrc.getDouble( "longitude" );
    d_dLatitude = strSrc.getDouble( "latitude" );
    d_association = new TKey<Association>( Association.class, strSrc.getString( "association" ) );

    if ( strSrc.isPropertySet( "current_manager" ) )
      d_currentManager = new TKey<Manager>( Manager.class, strSrc.getString( "current_manager" ) );

    if ( strSrc.isPropertySet( "current_league" ) )
      d_currentLeague = new TKey<Contest>( Contest.class, strSrc.getString( "current_league" ) );
  }

  @Override
  public void toGwt( JSData dest, DAO dao, String strParent )
  {
    toGwtMinimal( dest, dao, strParent );

    dest.set( "longitude", d_dLongitude );
    dest.set( "latitude", d_dLatitude );

    if ( d_currentManager != null )
      dest.set( "current_manager", d_currentManager.getName() );

    if ( d_currentLeague != null )
      dest.set( "current_league", d_currentLeague.getName() );
  }

  @Override
  public void toGwtMinimal( JSData dest, DAO dao, String strParent )
  {
    dest.set( JSData.PARAM_ID, d_strId );
    dest.set( "name", d_strName );
    dest.set( "country", d_strCountry );
    dest.set( "association", d_association.getName() );
  }
}
